﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UI.WebPage.Background;
using Model.AuthModel;
using BLL.AuthModel;
using Com.Ompchina.Lib.Text;
namespace Com.Ompchina.Web.admin.system
{
    public partial class add_func : AddInfoPage, IAddInfo
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && PageAction.Equals("update")) { this.InitSingleInfo(this.GetUpdateID()); }
        }

        #region IAddPage 成员

        public bool CheckInput()
        {
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                base.SetMessage(false, "请输入功能名称");
                return false;
            }
            else if (!(new Regular().ReguUsername(txtName.Text.Trim(), 1, 19)))
            {
                base.SetMessage(false, "功能名称只能由字母和数字组成");
                return false;
            }
            else if (string.IsNullOrEmpty(txtDescription.Text.Trim()))
            {
                base.SetMessage(false, "请输入功能描述");
                return false;
            }
            return true;
        }

        public void InitSingleInfo(int id)
        {
            if (id > 0)
            {
                FuncInfo info = new FuncBLL().GetFuncByID(id);
                if (info.FuncID > 0)
                {
                    hfID.Value = id.ToString();
                    txtName.Text = info.FuncName;
                    txtDescription.Text = Server.HtmlDecode(info.FuncDescription);
                    cbMenu.Checked = info.FuncMenu.Equals("y");
                }
            }    
        }

        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.CheckInput())
            {
                FuncBLL bll = new FuncBLL();
                if (hfID.Value.Equals("0"))
                {
                    if (bll.InsertFunc(Server.HtmlEncode(txtName.Text.Trim()), Server.HtmlEncode(txtDescription.Text.Trim())
                        , cbMenu.Checked ? "y" : "n"))
                    {
                        base.SetMessage(true, "新增功能成功");
                        base.Redirect("list_func.aspx?action=list", 1);
                    }
                    else
                        base.SetMessage(false, "新增功能错误，请稍后重试");
                }
                else
                {
                    if (bll.UpdateFunc(int.Parse(hfID.Value)
                        ,Server.HtmlEncode(txtName.Text.Trim()), Server.HtmlEncode(txtDescription.Text.Trim())
                                           , cbMenu.Checked ? "y" : "n"))
                    {
                        base.SetMessage(true, "修改功能成功");
                        base.Redirect("list_func.aspx?action=list", 1);
                    }
                    else
                        base.SetMessage(false, "修改功能错误，请稍后重试");
                }
            }
        }
    }
}
